package email;



import java.io.File;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Comparator;
import java.util.GregorianCalendar;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

public class ReceivedMessage{
	/**
	 * subject line of the message
	 */
	private String subject;
	/**
	 * date the message was received 
	 */
	private String date;
	
	private Date dateDate;
	
	/**
	 * the id of the ReceivedMessage, note this is not the id that comes with the Javamail class Message,
	 * we must provide the our own id system
	 */
	private long id;
	/**
	 * the email address of the person who sent the email to us
	 */
	private String from;
	/**
	 * The text in the body of the message
	 */
	private String body;
	/**
	 * The email addresses that are receiving this email
	 */
	private String recipients;
	private ArrayList<File> files;
	
	public ReceivedMessage(){
		subject = "";
		date = "";
		id = 0;
		from = "";
		body = "";
		recipients = "";
		files = new ArrayList<File>();
	}
	
	/**
	 * Constructs the ReceivedMessage class from a given javamail Message and a given id
	 * @param m the javamail Message that this object should represent
	 * @param l the id that is associated with this message
	 */
	public ReceivedMessage(Message m, long l){
		this.id = l;
		
		//call the private helper methods to set up the global variables of this message
		setSubject(m);
		setDate(m);
		setFrom(m);
		setBody(m);
		setRecipients(m);
	}
	
	/**
	 * Copy constructor
	 * @param other the other RecievedMessage that should be copied
	 */
	public ReceivedMessage(ReceivedMessage other){
		id = other.getId();
		subject = other.getSubject();
		body = other.getBody();
		date = other.getDate();
		from = other.getFrom();
		recipients = other.getRecipients();
		files = other.files;
	}
	
	/**
	 * Getter method for the subject
	 * @return the subject line of the method
	 */
	public String getSubject(){
		return subject;
	}
	
	/**
	 * Private method that gets the subject of the Message
	 * @param m the javamail Message object the subject is associated with
	 */
	private void setSubject(Message m){
		String toSet = "";
		try{
			toSet = m.getSubject();
		}
		catch (MessagingException e){
			//do nothing will just set a blank string
		}
		subject = toSet;
	}
	
	public void setSubject(String subject){
		this.subject = subject;
	}
	
	/**
	 * uses 24 hour clock
	 * Date format is like this Wed Nov 02 02:13:14 CDT 2011
	 * @return the date as a string
	 */
	public String getDate(){
		return date;
	}
	
	public Date getDateDate() {
		DateObject obj = new DateObject(this);
		Calendar.getInstance().set(obj.getYear(), obj.getMonth(), obj.getDay(), obj.getHours(), obj.getMinutes(), obj.getSeconds());
		return Calendar.getInstance().getTime();
	}
	
	
	
	/**
	 * Helper method to get the date from the object
	 * @param m the javamail message that the date received should be pulled from
	 */
	private void setDate(Message m){
		String toSet = "";
		try{
			//format it comes back as is Wed Nov 02 02:13:14 CDT 2011
			toSet = m.getReceivedDate().toString();
		}
		catch (MessagingException e){
			//do nothing blank date will be set
		}
		date = toSet;
	}
	
	public void setDate(String date){
		this.date = date;
		DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
		try {
			this.dateDate = fmt.parse(date);
		} catch (ParseException e) {
			this.dateDate = new Date(); //sets it to today.
		}
	}
	
	public void setDate(Date date) {
		this.dateDate = date;
	}
	
	/**
	 * Allows the id of the message to be set to a new id
	 * @param newId the new id to be associated with this message
	 */
	public void setId(long newId){
		id = newId;
	}
	
	/**
	 * Getter method that returns the id associated with this message
	 * @return the current id of this message
	 */
	public long getId(){
		return id;
	}
	
	/**
	 * Getter method that returns who this email was sent by
	 * @return the email address of the person who sent the email
	 */
	public String getFrom(){
		return from;
	}
	
	/**
	 * Helper method that gets the email address of the person who sent the email
	 * @param m the javamail Message that the address should be retrieved from
	 */
	private void setFrom(Message m){
		String toSet = "";
		try{
			//the address is returned in an array, the first one is taken because as far as I know only
			//one address can send an email so this array should always be one in length
			Address[] address = m.getFrom();
			toSet = address[0].toString();
		}
		catch (MessagingException e){
			//do nothing just leave from as blank
		}
		from = toSet;
	}
	
	public void setFrom(String from){
		this.from = from;
	}
	
	/**
	 * Getter method that returns the body text of the message associated with this object
	 * @return the body text of the message
	 */
	public String getBody(){
		return body;
	}
	
	/**
	 * Helper method that sets the content of the body
	 * @param m the javamail Message that is associated with this object
	 */
	private void setBody(Message m){
		String contentReturn;
		String contentType = "";
		Object content;
		try{
			content = m.getContent();
			contentReturn = m.getContentType();
			
			contentType = determineContentType(contentReturn);
			System.out.println("debug[setBody]:: contentType="+contentType);
			if (contentType.equals("TEXT/PLAIN")){
				body = (String) content;
			}
			else if(contentType.toLowerCase().equals("multipart/alternative")){
				MimeMultipart part = (MimeMultipart)content;
				MimeBodyPart bodypart = (MimeBodyPart)part.getBodyPart(0);
				body = (String)bodypart.getContent();
			}
			else{
				body = "";
			}
			System.out.println("dubug[setBody]:: body="+body);
		}
		catch (MessagingException e){
			
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void setBody(String body){
		this.body = body;
	}
	
	/**
	 * Takes a contentType string and cuts all the extra's besides the content type
	 * @param type the contentType string
	 * @return the content type without the extra at the end
	 */
	private String determineContentType(String type){
		String ret = "";
		
		int i = 0;
		//look for a semicolon or the end of the String
		while (i < type.length() && type.charAt(i) != ';'){
			ret += type.charAt(i);
			i++;
		}
		return ret;
	}
	
	
	
	
	public File[] getAttachments(){
		//TODO
		return new File[1];
	}
	
	private void setAttachments(Message m){
		//TODO
	}
	
	/**
	 * Getter method that returns the recipients of the message
	 * @return the recipients of the message currently using googles somewhat random formatting
	 */
	public String getRecipients(){
		return recipients;
	}
	
	/**
	 * Gets the recipients of the message and stores them
	 * @param m the javamail Message associated with this object
	 */
	private void setRecipients(Message m){
		recipients = "";
		try{
			Address[] address = m.getAllRecipients();
			int i = 0;
			while (i < address.length){
				recipients += address[i].toString() + ", ";
				i++;
			}
		}
		catch (MessagingException e){
			
		}
		catch(NullPointerException e){
			//leave recipents blank this situation occurs when iastate.edu send you something and I don't
			//know how we can handle it if the address is coming back null
		}
	}
	
	public void setRecipients(String recipients){
		this.recipients = recipients;
	}
	
	public String toString() {
		String ret = "Message: " + id + "; " + subject;
		return ret;
	}
}
